;(function($){
    var Affix=function(element,offset){
        this.elem=$(element);
        this.offset=parseInt(offset);
        this.pos=this.elem.offset();
        this.$win  = $( window ).on('scroll', $.proxy( this.posCheck, this ));
    };
    Affix.prototype={
        constructor :Affix,
        posCheck:function(){
            var t=this,
                scrollTop=this.$win.scrollTop();
            if(this.pos.top -scrollTop <=this.offset){
                this.elem.addClass('fixed')
            }else{
                this.elem.removeClass('fixed')
            }
        }

    };

    var affix=new Affix('.aside',40);


    var scrollSpy = function( element,offset ) {
        this.$node = $( element ).on("click", $.proxy(this.click,this));
        this.$anchor = this.$node.find('a');
        this.offset=parseInt(offset);
        this.$win = $(window).on("scroll", $.proxy(this.spy, this));
    }

    scrollSpy.prototype = {
        constructor : scrollSpy,
        nodeTop : function ( elem ) {
            var id = elem.attr("href");
            return $(id).offset().top - this.offset;
        },
        click : function(e) {
            e.preventDefault();
            var t = this;
            if (e.target.nodeName == "A") {
                $('html, body').stop().animate({
                    scrollTop : t.nodeTop($(e.target))
                },400);
            }

        },
        spy : function () {
            var t = this;
            var scrollTop = this.$win.scrollTop();
            for ( var i=0, len = this.$anchor.length; i < len; i++) {
                var sect = $(this.$anchor[i]);
                var top  = t.nodeTop(sect);
                if ( scrollTop > top - t.offset) {
                    this.$anchor.parent().removeClass("cur");
                    sect.parent().addClass("cur");
                }

            }
        }
    }

    new scrollSpy(".aside",40);


})(window.jQuery);
